#include <windows.h>
#include "common.h"

const char g_szClassName[] = "myWindowClass";
struct params PARAMS;
POINT RHOMB_POINTS[] = {{0, -20}, {-20, 0}, {0, 20}, {20, 0}, {0, -20}};
BYTE RHOMB_ORDER[] = {PT_MOVETO, PT_LINETO, PT_LINETO, PT_LINETO, PT_LINETO};
POINT STAR_POINTS[] = {{-10, -20}, {0, 15}, {10, -20}, {-20, 5}, {20, 5}, {-10, -20}};
BYTE STAR_ORDER[] = {PT_MOVETO, PT_LINETO, PT_LINETO, PT_LINETO, PT_LINETO, PT_LINETO, PT_LINETO};


LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	COLORREF color;
	COPYDATASTRUCT *cds;
	HDC hdc;

	switch(msg)
	{
		case WM_COPYDATA:
			cds = (COPYDATASTRUCT *)lParam;
			PARAMS = (struct params)*cds->lpData;
			return TRUE;
		case WM_LBUTTONDOWN:
			if (!PARAMS.draw)
				return 0;

			hdc = GetDC(hWnd);

			switch (PARAMS.colorid)
			{
				case 10001:
					color = RGB(255, 0, 0);
					break;
				case 10002:
					color = RGB(0, 255, 0);
					break;
				case 10003:
					color = RGB(0, 0, 255);
					break;
				default:
					return 0;
			}

			HPEN hPen = CreatePen(PS_SOLID, 1, color);
			SelectObject(hdc, hPen);
			/*SetBkColor(hdc, RGB(0, 0, 0));*/

			POINT cursorPos;
			if (!GetCursorPos(&cursorPos))
				return 0;
			if (!ScreenToClient(hWnd, &cursorPos))
				return 0;

			int i;
			POINT rhomb[5];
			POINT star[6];
			switch (PARAMS.shapeid)
			{
				case 10004:
					for (i = 0; i <= 4; ++i)
					{
						rhomb[i].x = cursorPos.x + RHOMB_POINTS[i].x;
						rhomb[i].y = cursorPos.y + RHOMB_POINTS[i].y;
					}
					PolyDraw(hdc, rhomb, RHOMB_ORDER, 5);
					break;
				case 10005:
					Rectangle(hdc, cursorPos.x-20, cursorPos.y-20, cursorPos.x+20, cursorPos.y+20);
					break;
				case 10006:
					Ellipse(hdc, cursorPos.x-20, cursorPos.y-20, cursorPos.x+20, cursorPos.y+20);
					break;
				case 10007:
					for (i = 0; i <= 5; ++i)
					{
						star[i].x = cursorPos.x - STAR_POINTS[i].x;
						star[i].y = cursorPos.y - STAR_POINTS[i].y;
					}
					PolyDraw(hdc, star, STAR_ORDER, 6);
					break;
			}

			ReleaseDC(hWnd, hdc);
			break;
		case WM_CLOSE:
			DestroyWindow(hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	wc.cbSize        = sizeof(WNDCLASSEX);
	wc.style         = 0;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
				MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(
			WS_EX_CLIENTEDGE,
			g_szClassName,
			"Draw",
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
			NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
				MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}
